package com.wwwarehouse.taskcenter.fragment.job_point.step5;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep5LookItemMesResponseBean;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep5SelectbyNameResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/25 18:53
 * @描述 ${TODO}
 */
public class ChooseJobUnitAllMessagePagerFragment5 extends CommonBasePagerFragment implements View.OnClickListener {

    private Button mBtSave;
    public static final int REQUEST_1 = 1003;
    private AddJobPointStep5SelectbyNameResponseBean listBean;
    private ArrayList<AddJobPointStep5SelectbyNameResponseBean.ListBean> selectListBean = new ArrayList<>();
//    private AddJobPointStep5SelectbyNameResponseBean selectBean = new AddJobPointStep5SelectbyNameResponseBean();

    private Map<String,AddJobPointStep5SelectbyNameResponseBean.ListBean> listBeanMap = new HashMap<>();
    private String jobPointUkid;
    private String warehouseUkid;
    private String searchKey = "";
    String asc = "create_time asc";
    String desc = "create_time desc";
    String mSort = desc;
    private int mSelectedBubblePosition = 2;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseJobUnitAllMessagePagerFragment5) {
            mActivity.setTitle("" + getString(R.string.new_task_step5_title_txt));
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        jobPointUkid = getArguments().getString("jobPointUkid");
        warehouseUkid = getArguments().getString("warehouseUkid");
        if(StringUtils.isNullString(warehouseUkid)){
            warehouseUkid = BaseApplication.sp.getValue("warehouseUkid");
        }

        showConfirmButton();
        setConfirmBtnText("" + getString(R.string.new_task_save));
        mBtSave = (Button) getConfirmBtn();
        mBtSave.setOnClickListener(this);
        ((BaseCardDeskActivity)mActivity).hideFilterBt();
        requestHttp();
        EventBus.getDefault().register(this);
    }

    @Override
    public void searchNoRealTimeClick(String text) {
        searchKey = text;
        requestHttp();
    }

    @Override
    public void cancelClick() {
        //取消按钮点击
        searchKey = "";
        requestHttp();
    }

    @Override
    public void sortClick(View anchor) {

        PopUpUtils.showBubbleInviteSort(mActivity, anchor, mSelectedBubblePosition, new PopUpUtils.OnChoiceListener() {
            @Override
            public void onChoice(int position) {
                mSelectedBubblePosition = position;
                switch (position) {
                    case 2:
                        //倒序
                        mSort = desc;
                        break;
                    case 3:
                        //正序
                        mSort = asc;
                        break;
                    default:
                        break;
                }

                requestHttp();
            }
        }, "",mActivity.getResources().getString(R.string.new_task_step6_time_desc),  mActivity.getResources().getString(R.string.new_task_step6_time_asc));
    }

    private void initPager() {
        Bundle budle = new Bundle();

        AddJobPointStep5SelectbyNameResponseBean.ListBean listBeanPeople = null;
        if(searchKey.isEmpty() || "人".equals(searchKey)){
            listBeanPeople  = new AddJobPointStep5SelectbyNameResponseBean.ListBean();
            listBeanPeople.setUnitUkid("");
            listBeanPeople.setUnitName("个");
            listBeanPeople.setAbstractObjectUkid("999");
            listBeanPeople.setName("人");
        }

        ArrayList<AddJobPointStep5SelectbyNameResponseBean.ListBean> lists = new ArrayList<>();
        if(listBean!=null){
            lists = listBean.getList();
            if(listBeanPeople!= null ){
                lists.add(0,listBeanPeople);
            }
        }

        try {
            setData(lists.size(), 10, "com/wwwarehouse/taskcenter/fragment/job_point/step5/ChooseJobUnitAllMessagePagerDetailDetailFragment5", budle, lists);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.showErrLog("error:"+e.getMessage());
        }
    }


    @Override
    public void onRefreshListener() {
        requestHttp();
    }

    @Override
    public void onLoadSuccess(String s, int i) {
        LogUtils.showErrLog("" + s);
        if (REQUEST_1 == i) {
            listBean = JSON.parseObject(s,AddJobPointStep5SelectbyNameResponseBean.class);
            initPager();
        }
    }

    @Override
    public void onLoadError(String s) {

    }

    /**
     *  查询名称
     */
    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("name",searchKey);
        map.put("type","H_TOOLS");
        try {
            map.put("ownerUkid",Long.parseLong(BaseApplication.sp.getValue(TaskCenterConstant.CONSTANT_BUSINESSID)));
        } catch (NumberFormatException e) {
        }

        Map<String ,Object >  queryMap = new HashMap<>();
        queryMap.put("size",-1);
        queryMap.put("sort",mSort);

        map.put("query",queryMap);

        loadData(TaskCenterConstant.URL_ADD_JOBPOINT_STEP5_GET_HAVEUNIT, map, REQUEST_1);
    }

    @Override
    public void onClick(View v) {
        if (v == mBtSave) {

            listBeanMap.keySet();
            selectListBean.clear();
            for(String str : listBeanMap.keySet()){
                selectListBean.add(listBeanMap.get(str));
            }

            AddJobPointStep5LookItemMesResponseBean select  = new AddJobPointStep5LookItemMesResponseBean();
            ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean> list = new ArrayList<>();
            AddJobPointStep5LookItemMesResponseBean.ListBean beanIner;

            for(AddJobPointStep5SelectbyNameResponseBean.ListBean listBean:selectListBean ){
                beanIner = new AddJobPointStep5LookItemMesResponseBean.ListBean();
                beanIner.setMetaCategoryUkid(listBean.getAbstractObjectUkid());
                beanIner.setCategoryName(listBean.getName());
                beanIner.setUnitName(listBean.getUnitName());
                beanIner.setUnitUkid(listBean.getUnitUkid());
                list.add(beanIner);
            }
            select.setList(list);

//            selectBean.setList(selectListBean);

            //确定
            Bundle bundle = new Bundle();
            bundle.putString("jobPointUkid",jobPointUkid);
            bundle.putParcelable("selectBean" , select);
            ChooseJobUnitSetnumberpagerFragment5  fragment5 = new ChooseJobUnitSetnumberpagerFragment5();
            fragment5.setArguments(bundle);
            pushFragment(fragment5);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(AddJobPointStep5SelectbyNameResponseBean.ListBean bean) {

        if(listBeanMap.containsKey(bean.getAbstractObjectUkid())){ // 含有就删除 不含有添加
            Iterator<Map.Entry<String, AddJobPointStep5SelectbyNameResponseBean.ListBean>> iterator = listBeanMap.entrySet().iterator();
            while (iterator.hasNext()){
                if((""+bean.getAbstractObjectUkid()).equals(iterator.next().getValue().getAbstractObjectUkid())){
                    iterator.remove();
                }
            }
        }else{
            listBeanMap.put(""+ bean.getAbstractObjectUkid(),bean);
        }
    }

}
